package com.ruoyi.jjkc.jjkcsq.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.infoHiComment.domain.InfoHiComment;
import com.ruoyi.infoRunTaskData.domain.InfoRunTaskData;
import com.ruoyi.infoRunTaskData.service.IInfoRunTaskDataService;
import com.ruoyi.jjkc.jdjh.domain.Jdjh;
import com.ruoyi.jjkc.jdjh.service.IJdjhService;
import com.ruoyi.jjkc.jjkcsq.domain.Jjkcsq;
import com.ruoyi.jjkc.jjkcsq.domain.JjkcsqFj;
import com.ruoyi.jjkc.jjkcsq.service.IJjkcsqService;
import com.ruoyi.jjkc.qyjbxx.domain.Qyjbxx;
import com.ruoyi.jjkc.qyjbxx.service.IQyjbxxService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import java.util.List;

/**
 * 加计扣除申请Controller
 *
 * @author huacai
 * @date 2024-12-08
 */
@RestController
@RequestMapping("/jjkc/jjkcsq")
public class JjkcsqController extends BaseController {
    @Resource
    private IJjkcsqService jjkcsqService;

    @Resource
    private IJdjhService jdjhService;

    @Resource
    private IQyjbxxService qyjbxxService;

    @Resource
    private IInfoRunTaskDataService iInfoRunTaskDataService;

    /**
     * 查询加计扣除申请列表
     */
    @PreAuthorize("@ss.hasPermi('jjkc:jjkcsq:list')")
    @GetMapping("/list")
    public TableDataInfo list(Jjkcsq jjkcsq) {
        startPage();
        List<Jjkcsq> list = jjkcsqService.selectJjkcsqList(jjkcsq);
        return getDataTable(list);
    }

    /**
     * 导出加计扣除申请列表
     */
    @PreAuthorize("@ss.hasPermi('jjkc:jjkcsq:export')")
    @Log(title = "加计扣除申请", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Jjkcsq jjkcsq) {
        List<Jjkcsq> list = jjkcsqService.selectJjkcsqList(jjkcsq);
        ExcelUtil<Jjkcsq> util = new ExcelUtil<Jjkcsq>(Jjkcsq.class);
        util.exportExcel(response, list, "加计扣除申请数据");
    }

    /**
     * 下载模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<Jjkcsq> util = new ExcelUtil<Jjkcsq>(Jjkcsq.class);
        util.importTemplateExcel(response, "加计扣除申请数据");
    }

    /**
     * 导入数据
     */
    @Log(title = "加计扣除申请", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('jjkc:jjkcsq:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<Jjkcsq> util = new ExcelUtil<Jjkcsq>(Jjkcsq.class);
        InputStream inputStream = file.getInputStream();
        List<Jjkcsq> list = util.importExcel(inputStream);
        inputStream.close();
        int count = jjkcsqService.batchInsertJjkcsq(list);
        return AjaxResult.success("导入成功" + count + "条信息！");
    }

    /**
     * 获取加计扣除申请详细信息
     */
    @PreAuthorize("@ss.hasPermi('jjkc:jjkcsq:query')")
    @GetMapping(value = "/{jjkcsqId}")
    public AjaxResult getInfo(@PathVariable("jjkcsqId") String jjkcsqId) {
        return success(jjkcsqService.selectJjkcsqByJjkcsqId(jjkcsqId));
    }

    /**
     * 新增加计扣除申请
     */
    @PreAuthorize("@ss.hasPermi('jjkc:jjkcsq:add')")
    @Log(title = "加计扣除申请", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Jjkcsq jjkcsq) {
        jjkcsq.setUserId(Math.toIntExact(getUserId())); //用户ID
        jjkcsq.setDeptId(Math.toIntExact(getDeptId())); //部门ID
        jjkcsq.setCreateBy(getUsername()); //创建人
        jjkcsq.setIdWhere("存在");

        //上年度研发费实际发生额
        BigDecimal sndYffSjfs = jjkcsq.getSndYffSjfs();
        // 定义转换因子，即1万元中有多少元
        BigDecimal conversionFactor = new BigDecimal("10000");
        // 设定一个小数点后位数的上限，比如20位，以防止无限小数
        int scale = 20;
        // 进行转换，不进行四舍五入，直接截断
        jjkcsq.setSndYffSjfs(sndYffSjfs.divide(conversionFactor, scale, RoundingMode.DOWN));

        //已存在加计扣除申请列表中的鉴定计划ID, 不可再在选择加计扣除项目列表中存在
        Jdjh jdjh = new Jdjh();
        jdjh.setJdjhId(jjkcsq.getJdjhId());
        jdjh.setSfzSqLb("是");
        jdjhService.updateJdjh(jdjh);

        //根据鉴定年度查询企业基本信息对应年度的企业信息
        Qyjbxx qyjbxx = qyjbxxService.selectQyjbxxByNd(jjkcsq.getJdNd());
        jjkcsq.setQyName(qyjbxx.getQyName()); //企业名称
        jjkcsq.setFrDb(qyjbxx.getFrDb()); //法人代表
        jjkcsq.setTxDz(qyjbxx.getTxDz()); //通讯地址
        jjkcsq.setQyLxDh(qyjbxx.getQyLxDh()); //企业联系电话（座机）
        jjkcsq.setQySsGmjjHy("行业名称：" + qyjbxx.getHyMc() + "\t\t" + "类别：" + qyjbxx.getLb()); //企业所属国民经济行业
        jjkcsq.setQyLx(qyjbxx.getQyLx()); //企业类型
        jjkcsq.setZgZrs(qyjbxx.getZgZrs()); //职工总人数
        jjkcsq.setYfRys(qyjbxx.getYfRys()); //研发人员数
        jjkcsq.setNdXsSr(qyjbxx.getNdXsSr()); //年度销售收入（万元）
        jjkcsq.setNdLs(qyjbxx.getNdLs()); //年度利税（万元）
        jjkcsq.setLxr(qyjbxx.getLxr()); //联系人
        jjkcsq.setLxDh(qyjbxx.getLxDh()); //联系电话

        jjkcsq.setJjkcsqId(IdUtils.fastSimpleUUID());
        int i = jjkcsqService.insertJjkcsq(jjkcsq);

        //向流程业务表中加入数据
        InfoRunTaskData infoRunTaskData = new InfoRunTaskData();
        infoRunTaskData.setBusinessKey(jjkcsq.getJjkcsqId());
        infoRunTaskData.setBusinessType(13); //业务模块: 加计扣除-加计扣除申请
        infoRunTaskData.setBusinessTitle(jjkcsq.getXmName() + '-' + jjkcsq.getXmBh()); //任务名称: 项目名称-项目编号
        infoRunTaskData.setApplyUser(SecurityUtils.getLoginUser().getUser().getNickName());//申请人

        iInfoRunTaskDataService.insertInfoRunTaskData(infoRunTaskData);

        return toAjax(i);
    }

    /**
     * 修改加计扣除申请
     */
    @PreAuthorize("@ss.hasPermi('jjkc:jjkcsq:edit')")
    @Log(title = "加计扣除申请", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Jjkcsq jjkcsq) {
        //修改业务中间表关系数据
        InfoRunTaskData infoRunTaskData = new InfoRunTaskData();
        infoRunTaskData.setBusinessTitle(jjkcsq.getXmName() + '-' + jjkcsq.getXmBh()); //任务名称: 项目名称-项目编号
        infoRunTaskData.setBusinessKey(jjkcsq.getJjkcsqId());
        infoRunTaskData.setBusinessType(13); //业务模块: 加计扣除-加计扣除申请
        iInfoRunTaskDataService.updateInfoRunTaskDatas(infoRunTaskData); //注意updateInfoRunTaskDatas

        return toAjax(jjkcsqService.updateJjkcsq(jjkcsq));
    }

    /**
     * 鉴定通过
     */
    @Log(title = "加计扣除项目鉴定通过", businessType = BusinessType.UPDATE)
    @PutMapping("/updateJdTg")
    public AjaxResult updateJdTg(@RequestBody Jjkcsq jjkcsq) {
        //修改业务中间表关系数据
        InfoRunTaskData infoRunTaskData = new InfoRunTaskData();
        infoRunTaskData.setBusinessTitle(jjkcsq.getXmName() + '-' + jjkcsq.getXmBh()); //任务名称: 项目名称-项目编号
        infoRunTaskData.setBusinessKey(jjkcsq.getJjkcsqId());
        infoRunTaskData.setBusinessType(13); //业务模块: 加计扣除-加计扣除申请
        int i = iInfoRunTaskDataService.updateInfoRunTaskDatas(infoRunTaskData);

        //修改业务数据状态为完成 鉴定通过
        iInfoRunTaskDataService.updateStatus(jjkcsq.getJjkcsqId(), 13, 5);
        return toAjax(i);
    }

    /**
     * 鉴定未通过
     */
    @Log(title = "加计扣除项目鉴定未通过", businessType = BusinessType.UPDATE)
    @PutMapping("/updateJdWtg")
    public AjaxResult updateJdWtg(@RequestBody Jjkcsq jjkcsq) {
        //修改业务中间表关系数据
        InfoRunTaskData infoRunTaskData = new InfoRunTaskData();
        infoRunTaskData.setBusinessTitle(jjkcsq.getXmName() + '-' + jjkcsq.getXmBh()); //任务名称: 项目名称-项目编号
        infoRunTaskData.setBusinessKey(jjkcsq.getJjkcsqId());
        infoRunTaskData.setBusinessType(13); //业务模块: 加计扣除-加计扣除申请
        int i = iInfoRunTaskDataService.updateInfoRunTaskDatas(infoRunTaskData);

        //修改业务数据状态为完成 鉴定未通过
        iInfoRunTaskDataService.updateStatus(jjkcsq.getJjkcsqId(), 13, 6);
        return toAjax(i);
    }

    /**
     * 删除加计扣除申请
     */
    @PreAuthorize("@ss.hasPermi('jjkc:jjkcsq:remove')")
    @Log(title = "加计扣除申请", businessType = BusinessType.DELETE)
    @DeleteMapping("/{jjkcsqIds}")
    public AjaxResult remove(@PathVariable String[] jjkcsqIds) {
        //删除加计扣除申请项的同时, 将对应的jdjhId的 计划鉴定项的 是否在申请列表字段 改为 否
        for (String jjkcsqId : jjkcsqIds) {
            Jjkcsq jjkcsq = jjkcsqService.selectJjkcsqByJjkcsqId(jjkcsqId);
            Integer jdjhId = jjkcsq.getJdjhId();
            Jdjh jdjh = new Jdjh();
            jdjh.setJdjhId(jdjhId);
            jdjh.setSfzSqLb("否");
            jdjhService.updateJdjh(jdjh);
        }
        //同时删除业务关系数据
        for (String jjkcsqId : jjkcsqIds) {
            Jjkcsq jjkcsq = jjkcsqService.selectJjkcsqByJjkcsqId(jjkcsqId);
            iInfoRunTaskDataService.deleteData(jjkcsq.getJjkcsqId(), 13);//业务模块: 加计扣除-加计扣除申请
        }
        return toAjax(jjkcsqService.deleteJjkcsqByJjkcsqIds(jjkcsqIds));
    }

    /**
     * 新增附件
     */
    @PostMapping("/add/File/By/subId")
    public AjaxResult insertJjkcsqFjByJjkcsqId(@RequestBody JjkcsqFj jjkcsqFj) {
        return toAjax(jjkcsqService.insertJjkcsqFjByJjkcsqId(jjkcsqFj));
    }

    /**
     * 根据ID查询附件列表
     */
    @GetMapping("/get/FileList/By/subId")
    public AjaxResult selectJjkcsqFjList(String sId, String fileType) {
        JjkcsqFj jjkcsqFj = new JjkcsqFj();
        jjkcsqFj.setJjkcsqId(sId);
        jjkcsqFj.setFileType(fileType);
        return AjaxResult.success(jjkcsqService.selectJjkcsqFjList(jjkcsqFj));
    }

    /**
     * 删除加计扣除申请附件
     */
    @Log(title = "加计扣除申请附件", businessType = BusinessType.DELETE)
    @DeleteMapping("/fj/{fjIds}")
    public AjaxResult deleteJjkcsqFjByFjIds(@PathVariable Integer[] fjIds) {
        return toAjax(jjkcsqService.deleteJjkcsqFjByFjIds(fjIds));
    }

}
